import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey



#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    

    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/distinct_fips.csv'
    countyInfo = build_data_list(filepath)


    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/test/Export_Output.csv'
    data = build_data_list(filepath)
    
    fips = []
    for item in countyInfo:
        fips.append(int(item[0]))

    for item in data:
        if int(item[0]) not in fips:
            print item[0]
    '''

    output = []
    nocentorid = []
        
    for item in countyInfo:
        if int(item[0]) in fips:
            #print int(item[0])
            output.append(item)
        else:
            nocentorid.append(int(item[0]))
    print nocentorid
    print len(fips), len(countyInfo), len(nocentorid), len(output), len(countyInfo)-len(nocentorid)-len(output)
    #np.savetxt(filepath[:-4] + '_id_1.csv', sKey, delimiter=',', fmt = '%f')
    '''
    '''

    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/test/distinct_fips_Continental.csv'
    countyInfo = build_data_list(filepath)

    fips_dic = {}
    for item in countyInfo:
        fips_dic[str(int(item[0]))] = int(item[1])

    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/test/county_centroid_continental.csv'
    #data = build_data_list(filepath)

    sKey = []
    fn = filepath
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
        sKey.append(-1)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames)+1)

    for item in sKey:
        item[-1] = fips_dic[str(int(item[0]))]


    
    np.savetxt(filepath[:-4] + '_id_1.csv', sKey, delimiter=',', fmt = '%f')
    '''
    